<?php

namespace app\common\framework;

use app\model\setting\TraceIDSetting;
use support\exception\BusinessException;
use Webman\Http\Request;
use Webman\Http\Response;

class HttpApiResponse extends BusinessException
{
    protected $data = null;


    public function __construct($code = 0, $message = '', $data = null, $previous = null)
    {
        $this->setData($data);
        parent::__construct($message, $code, $previous);
    }

    public function render(Request $request): ?Response
    {
        $code = $this->getCode();
        $json = ['code' => $code ?: 0, 'msg' => $this->getMessage(), 'data' => $this->getData(), "trace_id" => TraceIDSetting::instance()->getTraceID()];
        return new Response(200, ['Content-Type' => 'application/json'],
            json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));

    }
}